package ch.unizh.ifi.ailab.tests;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import ch.unizh.ifi.ailab.config.ProgramSettings;
import ch.unizh.ifi.ailab.dataTypes.SensorData;
import ch.unizh.ifi.ailab.utils.Utils;



/**
 * @author hans jaeckle jaeckle@gmail.com
 * 
 */
public class RegExTest {

	// logging
	private static Logger myLog = Logger.getLogger(RegExTest.class);

	private static final Pattern pattern0 = Pattern
			.compile("Infrarot Distanz-Sensoren.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>");

	private static final Pattern pattern1 = Pattern
			.compile("IO-Board Analog-Sensoren.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>.*?<b>(\\d+)</b>");

	private static String sensorSite = "<HTML> <HEAD> <TITLE>Wireless configuration</TITLE> </HEAD> <BODY bgcolor=#d0d0ff><font face=helvetica> <CENTER><H3>Koffer Steuerung</H3> <FORM action=wlan1.egi> <TABLE border=0 cellpadding=2 cellspacing=0> <TR><TD>Action:</TD> <TD COLSPAN=\"2\"> <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Update\"> </TD></TR> <TR></TR> <TR><TD>Richtung:</TD> <TD COLSPAN=\"3\"> <INPUT TYPE=\"text\" NAME=\"dir\" MAXLENGTH=\"3\" > </TD><TD>(Werte zwischen 222 und 236 erlaubt!)</TR> <TR><TD>Geschwindigkeit:</TD> <TD COLSPAN=\"3\"> <INPUT TYPE=\"text\" NAME=\"speed\" MAXLENGTH=\"3\" > </TD><TD>(Werte zwischen 19 und 35 erlaubt!)</TR> <TR><TD>Motor Stop-Limite f�r Sensoren:</TD> <TD COLSPAN=\"3\"> <INPUT TYPE=\"text\" NAME=\"stoplim\" MAXLENGTH=\"3\" > </TD><TD>(Werte zwischen 1 und 255 erlaubt!)</TR> </TR> </TABLE> <h3>Infrarot Distanz-Sensoren</h3><table border=6> <tr><td>S1</td><td><b>0</b></td> <tr><td>S1</td><td><b>0</b></td> <tr><td>S1</td><td><b>0</b></td> <tr><td>S1</td><td><b>0</b></td><tr> </tr></table> </TABLE> <h3>IO-Board Analog-Sensoren</h3><table border=6> <tr><td>S1</td><td><b>121</b></td> <tr><td>S2</td><td><b>121</b></td> <tr><td>S3</td><td><b>120</b></td> <tr><td>S4</td><td><b>120</b></td><tr> </tr></table> </TABLE> <h3>IO-Board Digital-Sensoren</h3><table border=6> <tr><td>S1</td><td><b>200</b></td><tr> </tr></table> </FORM> </CENTER></BODY></HTML>";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ProgramSettings.getProgramSettings().setProperty("path.conf",
				Utils.path(System.getProperty("user.dir"), "conf"));
		// configure logging
		PropertyConfigurator.configure(Utils.path(ProgramSettings
				.getProgramSettings().getProperty("path.conf"),
				"Null15Logger.conf"));
		SensorData data = new SensorData();
		// werte der ir sensoren
		Matcher matcher0 = pattern0.matcher(sensorSite);
		matcher0.find();
		data.setIr1(Integer.parseInt(matcher0.group(1)));
		data.setIr2(Integer.parseInt(matcher0.group(2)));
		data.setIr3(Integer.parseInt(matcher0.group(3)));
		data.setIr4(Integer.parseInt(matcher0.group(4)));

		// werte der io sensoren
		Matcher matcher1 = pattern1.matcher(sensorSite);
		data.setIo1(Integer.parseInt(matcher1.group(1)));
		data.setIo2(Integer.parseInt(matcher1.group(2)));
		data.setIo3(Integer.parseInt(matcher1.group(3)));
		data.setIo4(Integer.parseInt(matcher1.group(4)));

		// logging sensordata
		myLog.info("koffer: geparste daten: " + data.toString());
	}

}
